package com.lzx.iteam.net;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.ElementData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shijunting on 15-1-26.
 */
public class GetStructureMsg extends CcMsgStructure {

    private static final String TAG = "GetStructureMsg";

    private Message mmCallback;
    private Context mContext;
    private String mGid;

    public GetStructureMsg(Message callback, Context mContext,String mGid) {
        mmCallback = callback;
        this.mContext = mContext;
        this.mGid = mGid;
    }

    @Override
    public void onReceive(JSONObject data) {
        try {
            	SharedPreferences sp = mContext.getSharedPreferences("structure", Context.MODE_PRIVATE);
//            	String structureJson = sp.getString("json", "");
//            	if (!structureJson.equals(data.toString())) {
                ArrayList<ElementData> elementList = new ArrayList<ElementData>();
                ArrayList<ElementData> elementListTop = new ArrayList<ElementData>();
                JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
                JSONArray jsonArray = jsonObject.getJSONArray(AsynHttpClient.KEY_TAGS);
                int groupid = 0;
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject item = jsonArray.getJSONObject(i);
                    ElementData elementItem = new ElementData();
                    elementItem.contentText = item.getString(AsynHttpClient.KEY_STRUCTURE_NAME);
                    elementItem.id = item.getInt(AsynHttpClient.KEY_STRUCTURE_ID);
                    elementItem.contactCount = item.getInt(AsynHttpClient.KEY_STRUCTURE_CONTACT_COUNT);
                    elementItem.groupId = item.getInt(AsynHttpClient.KEY_GROUP_ID);
                    //elementItem.path = item.getString(AsynHttpClient.KEY_PATH);
                    elementItem.type=jsonObject.getString(AsynHttpClient.KEY_TYPE);
                    
                    groupid = item.getInt(AsynHttpClient.KEY_GROUP_ID);
                    /*
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString( item.getInt(AsynHttpClient.KEY_GROUP_ID)+"", data.toString());
                    editor.commit();
                    */
                    if (item.getString(AsynHttpClient.KEY_STRUCTURE_PID) == null || item.getString(AsynHttpClient.KEY_STRUCTURE_PID).equals("null")) {
                        elementItem.parendId = 0;
                    } else {
                        elementItem.parendId = item.getInt(AsynHttpClient.KEY_STRUCTURE_PID);
                    }
                    elementItem.level = item.getString(AsynHttpClient.KEY_STRUCTURE_PATH).split("/").length;
                    if (item.getInt(AsynHttpClient.KEY_STRUCTURE_CHILD_COUNT) == 0) {
                        elementItem.hasChildren = false;
                    } else {
                        elementItem.hasChildren = true;
                    }
                    elementItem.isExpanded = false;
                    elementList.add(elementItem);
                    if (elementItem.level == 1) {
                        elementListTop.add(elementItem);
                    }
                }
                if(elementList.size()<1){
                    ElementData elementItem = new ElementData();
                    elementItem.type=jsonObject.getString(AsynHttpClient.KEY_TYPE);
                    elementList.add(elementItem);
                }

                SharedPreferences.Editor editor = sp.edit();
                editor.putString( groupid+"", data.toString());
                //editor.commit();
                
                
                //SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean( this.mGid+"tag", true);
                editor.commit();

                if (mmCallback != null) {
                    Map<String, ArrayList<ElementData>> map = new HashMap<String, ArrayList<ElementData>>();
                    map.put("top", elementListTop);
                    map.put("all", elementList);
                    mmCallback.obj = map;
                    mmCallback.arg1 = 0;
                    mmCallback.sendToTarget();
                    return;
                }
//            } else {
//                return;
//            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }


    public static Map<String, ArrayList<ElementData>> getElementMsg(String json) {
        try {
            JSONObject data = new JSONObject(json);
            ArrayList<ElementData> elementList = new ArrayList<ElementData>();
            ArrayList<ElementData> elementListTop = new ArrayList<ElementData>();
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            JSONArray jsonArray = jsonObject.getJSONArray(AsynHttpClient.KEY_TAGS);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject item = jsonArray.getJSONObject(i);
                ElementData elementItem = new ElementData();
                elementItem.contentText = item.getString(AsynHttpClient.KEY_STRUCTURE_NAME);
                elementItem.id = item.getInt(AsynHttpClient.KEY_STRUCTURE_ID);
                elementItem.contactCount = item.getInt(AsynHttpClient.KEY_STRUCTURE_CONTACT_COUNT);
                elementItem.groupId = item.getInt(AsynHttpClient.KEY_GROUP_ID);
                //elementItem.path = item.getString(AsynHttpClient.KEY_PATH);
                elementItem.type=jsonObject.getString(AsynHttpClient.KEY_TYPE);
                if (item.getString(AsynHttpClient.KEY_STRUCTURE_PID) == null || item.getString(AsynHttpClient.KEY_STRUCTURE_PID).equals("null")) {
                    elementItem.parendId = 0;
                } else {
                    elementItem.parendId = item.getInt(AsynHttpClient.KEY_STRUCTURE_PID);
                }
                elementItem.level = item.getString(AsynHttpClient.KEY_STRUCTURE_PATH).split("/").length;
                if (item.getInt(AsynHttpClient.KEY_STRUCTURE_CHILD_COUNT) == 0) {
                    elementItem.hasChildren = false;
                } else {
                    elementItem.hasChildren = true;
                }
                elementItem.isExpanded = false;
                elementList.add(elementItem);
                if (elementItem.level == 1) {
                    elementListTop.add(elementItem);
                }
            }
            Map<String, ArrayList<ElementData>> map = new HashMap<String, ArrayList<ElementData>>();
            map.put("top", elementListTop);
            map.put("all", elementList);
            return map;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }
        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}
